﻿/**
* 上海XXX信息科技公司 版权所有 All Rights Reserved
* 
* @Author Irving_Zhou
* @Description JavaScript.Common
* @Date 2012年4月9日15:50:32
* @Version 1.0.0
* @Email zhouyongtao@outlook.com
* @Require this common plugin need jquery
*/
/*添加IE7 JSON支持 <!--[if lt IE 7]><script src="../js/json2.js" type="text/javascript"></script><![endif]-->*/
if (!window.JSON) {
    window.JSON = {
        parse: function (sJSON) { return eval("(" + sJSON + ")"); },
        stringify: function (vContent) {
            if (vContent instanceof Object) {
                var sOutput = "";
                if (vContent.constructor === Array) {
                    for (var nId = 0; nId < vContent.length; sOutput += this.stringify(vContent[nId]) + ",", nId++);
                    return "[" + sOutput.substr(0, sOutput.length - 1) + "]";
                }
                if (vContent.toString !== Object.prototype.toString) { return "\"" + vContent.toString().replace(/"/g, "\\$&") + "\""; }
                for (var sProp in vContent) { sOutput += "\"" + sProp.replace(/"/g, "\\$&") + "\":" + this.stringify(vContent[sProp]) + ","; }
                return "{" + sOutput.substr(0, sOutput.length - 1) + "}";
            }
            return typeof vContent === "string" ? "\"" + vContent.replace(/"/g, "\\$&") + "\"" : String(vContent);
        }
    };
}
//让firefox支持 event全局对象，srcElement对象  
function __firefox() {
    HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
    window.constructor.prototype.__defineGetter__("event", __window_event);
    Event.prototype.__defineGetter__("srcElement", __event_srcElement);
}
function __element_style() {
    return this.style;
}
function __window_event() {
    return __window_event_constructor();
}
function __event_srcElement() {
    return this.target;
}
function __window_event_constructor() {
    if (document.all) {
        return window.event;
    }
    var _caller = __window_event_constructor.caller;
    while (_caller != null) {
        var _argument = _caller.arguments[0];
        if (_argument) {
            var _temp = _argument.constructor;
            if (_temp.toString().indexOf("Event") !== -1) {
                return _argument;
            }
        }
        _caller = _caller.caller;
    }
    return null;
}
if (window.addEventListener) {
    __firefox();
}

/*添加到收藏夹*/
function addToFavorite() {
    var a = "http://www.baidu.com/";
    var b = "京东商城-网购上京东，省钱又放心";
    if (document.all) {
        window.external.AddFavorite(a, b)
    } else if (window.sidebar) {
        window.sidebar.addPanel(b, a, "")
    } else {
        alert("对不起，您的浏览器不支持此操作!\n请您使用菜单栏或Ctrl+D收藏本站。")
    }
}

/*获得滚动条位置*/
function getscrollTop() {
    return window.pageYOffset  //用于FF
                || document.documentElement.scrollTop
                || document.body.scrollTop
                || 0;
};
/*获得Event对象*/
function getEvent(event) {
    return event || window.event;
};
/*比较时间*/
function compareTime(first, second) {
    var first_time = first.split("-");
    var second_time = second.split("-");
    return new Date(first_time[0], first_time[1], first_time[2]).getTime() > new Date(second_time[0], second_time[1], second_time[2]).getTime() ? true : false
}
/*Just Test*/
$.fn.txtHover = function () {
    var _this = this;
    return this.each(function () {
        jQuery(this).hover(function () {
            jQuery(this).text("Just Test Mouse hovered");
        });
    });
};
/*自动缩放图片高度宽度*/
$.fn.ImgAutoSize = function (width, height) {
    $("img", this).each(function () {
        var image = $(this);
        if (image.width() > width) {
            image.width(width);
            image.height(width / image.width() * image.height());
        }
        if (image.height() > height) {
            image.height(height);
            image.width(height / image.height() * image.width());
        }
    });
}

/*
将表单对象转为JSON对象
*/
function formifyJSON(formName) {
    var data = $('#' + formName).serializeArray();
    var result = {};
    var multiChooseArray = result[arrName]; for (var formValue, j = 0; j < data.length; j++) {
        formValue = data[j];
        var name = formValue.name;
        var value = formValue.value;
        if (name.indexOf('.') < 0) {
            result[name] = value;
            continue;
        } else {
            var simpleNames = name.split('.');
            // 构建命名空间
            var obj = result;
            for (var i = 0; i < simpleNames.length - 1; i++) {
                var simpleName = simpleNames[i];
                if (simpleName.indexOf('[') < 0) {
                    if (obj[simpleName] === null) {
                        obj[simpleName] = {};
                    }
                    obj = obj[simpleName];
                } else { // 数组
                    // 分隔
                    var arrNames = simpleName.split('[');
                    var arrName = arrNames[0];
                    var arrIndex = parseInt(arrNames[1]);
                    if (obj[arrName] === null) {
                        obj[arrName] = []; // new Array();
                    }
                    obj = obj[arrName];
                    if (obj[arrIndex] === null) {
                        obj[arrIndex] = {}; // new Object();
                    }
                    obj = obj[arrIndex];
                }
            }
            if (obj[simpleNames[simpleNames.length - 1]]) {
                var temp = obj[simpleNames[simpleNames.length - 1]];
                obj[simpleNames[simpleNames.length - 1]] = temp;
            } else {
                obj[simpleNames[simpleNames.length - 1]] = value;
            }
        }
    }
    return result;
}